﻿//******************** WHUI控件库***外部角标控件 WHAngleMark 使用说明********************************************
//************1.相对与WHLable等拥有内部角标的控件本控件单独使用，更加灵活
//************2.支持填充色改变，支持显示文本颜色，字体等设置，支持外边框显示，和颜色转变
//************3.主要属性 为 MarkText=“要显示的字符串”
//************4.目前：控件大小为自动模式，即控件大小随 MarkText显示的字符，字体大小和多少自动填充变化，暂不支持自定义大小感觉意义不大
//************5.  其他设置项目看控件属性说明，
//************如有疑问联系 微信：13781551704******qq：453983184 ******************
//**********************If you use this code, please keep this note.
//****************** * 如果您使用此代码，请保留此说明。*******************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WHControlLib.Controls
{
    public partial class WHAngleMark : UserControl
    {
        public WHAngleMark()
        {
            //设置双缓冲开启禁止背景擦拭
            SetStyle(
                     ControlStyles.OptimizedDoubleBuffer
                     | ControlStyles.ResizeRedraw
                     | ControlStyles.Selectable
                     | ControlStyles.AllPaintingInWmPaint
                     | ControlStyles.UserPaint
                     | ControlStyles.SupportsTransparentBackColor,
                     true);


            ////背景定义为透明色   
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            //BackColor = Color.Transparent;

            InitializeComponent();
        }
        #region 属性定义
        private Color _markBackColor = Color.Red;
        [Category("A我的角标"), Description("控件上角标的背景色，默认 红色 "), Browsable(true)]
        public Color MarkBackColor
        {
            get { return _markBackColor; }
            set { _markBackColor = value; Invalidate(); }
        }
        private Color _markTextColor = Color.White;
        [Category("A我的角标"), Description("控件上角标上文字的颜色，默认 白色 "), Browsable(true)]
        public Color MarkTextColor
        {
            get { return _markTextColor; }
            set { _markTextColor = value; Invalidate(); }
        }

        private string _markText = "";
        [Category("A我的角标"), Description("控件上角标上文字 文本，默认 无 "), Browsable(true)]
        public string MarkText
        {
            get { return _markText; }
            set { _markText = value; Invalidate(); }
        }


        private bool _isShowMarkBorder=true;
        [Category("A我的角标"), Description("是否显示角标外形边框，最好设置true,不然会有轻微锯齿 ，默认 显示 "), Browsable(true)]
        public bool IsShowMarkBorder
        {
            get { return _isShowMarkBorder; }
            set { _isShowMarkBorder = value; Invalidate(); }
        }
        private int _markBorderWidth = 2;
        [Category("A我的角标"), Description("角标外形边框的宽度 ，默认 2"), Browsable(true)]
        public int MarkBorderWidth
        {
            get { return _markBorderWidth; }
            set { _markBorderWidth = value; Invalidate(); }
        }
        private Color _markBorderColor = Color.Red;
        [Category("A我的角标"), Description("角标外形边框的颜色 ，默认 白色 "), Browsable(true)]
        public Color MarkBorderColor
        {
            get { return _markBorderColor; }
            set { _markBorderColor = value; Invalidate(); }
        }
        private Color _markerTextColor = Color.White;
        [Category("A我的角标"), Description("角标文本的颜色 ，默认 白色 "), Browsable(true)]
        public Color MarkerTextColor
        {
            get { return _markerTextColor; }
            set { _markerTextColor = value; Invalidate(); }
        }


        private Font _markFont = new Font("微软雅黑,粗体", 10.0f);
        [Category("A我的角标"), Description("角标文本字体 ，默认 微软雅黑，8.0f "), Browsable(true)]
        public Font MarkFont
        {
            get { return _markFont; }
            set
            {
                _markFont = value;

                this.Invalidate();
            }
        }

        private float _markTextToBond=5.0f;
        [Category("A我的角标"), Description("角标文字与图形边界的距离 ，默认 5.0f,不能小于1 "), Browsable(true)]
        public float MarkTextToBond
        {
            get { return _markTextToBond; }
            set {
                if (value<1)
                {
                    _markTextToBond = 1;
                }
               else _markTextToBond= value;
                this.Invalidate();
            }
        }
        private int _markDelfuleSize = 10;
        [Category("A我的角标"), Description("角标没有文本时候的默认大小 ，默认 10像素,不能小于2 "), Browsable(true)]
        public int MarkDelfuleSize
        {
            get { return _markDelfuleSize; }
            set
            {
                if (value < 2)
                {
                    _markDelfuleSize = 2;
                }
                else _markDelfuleSize = value;
                this.Invalidate();
            }
        }

        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
           //Rectangle MyRect = this.ClientRectangle;
            Graphics Myg = e.Graphics;
            Myg.SmoothingMode = SmoothingMode.AntiAlias;
            Myg.CompositingQuality = CompositingQuality.HighQuality;
            Myg.InterpolationMode = InterpolationMode.HighQualityBicubic;
           
            Rectangle MyRect = GetAutoMyRect(Myg, MarkText);

            DrawMarkShape(Myg, MyRect);

            DrawMarkText(Myg, MyRect);
            this.BringToFront();

        }
        Rectangle GetAutoMyRect(Graphics myg, string MarkText)
        {
            Rectangle MyAutoRect    = new Rectangle();
           float textHeight, textWidth;
            if (MarkText!=""&&MarkText!=null)
            {
            
             textHeight= myg.MeasureString(MarkText, MarkFont).Height;
             textWidth = myg.MeasureString(MarkText, MarkFont).Width;
             if (textHeight>textWidth)
               {
               textWidth = textHeight ;
               }
            }
            else
            {
                textHeight = MarkDelfuleSize;
                textWidth = MarkDelfuleSize;
            }


            this.Height =(int) (textHeight + MarkTextToBond)+6+(int)MarkBorderWidth/2;
            this.Width = (int)(textWidth + MarkTextToBond) + 6 + (int)MarkBorderWidth / 2;
            MyAutoRect = this.ClientRectangle;
            return MyAutoRect;

        }
        void DrawMarkShape(Graphics myg,Rectangle myRect)
        {
            Rectangle MarkRect = new Rectangle();
            MarkRect.X = myRect.X +  2;
            MarkRect.Y=myRect.Y +  2;
            MarkRect.Width = myRect.Width- 4 ;
            MarkRect.Height=myRect.Height- 4 ;

            GraphicsPath MarkPath = new GraphicsPath();
            MarkPath = DrawHelper.GetTwoHalfCircleRect(MarkRect, MarkBorderWidth);
            Region reg=new Region(DrawHelper.GetTwoHalfCircleRect(myRect, MarkBorderWidth));

            this.Region = reg;
            myg.FillPath(new SolidBrush(MarkBackColor), MarkPath);
            if (IsShowMarkBorder)
            {
                myg.DrawPath(new Pen(MarkBorderColor, MarkBorderWidth), DrawHelper.GetTwoHalfCircleRect(MarkRect, MarkBorderWidth));
            }
        }
           
        void DrawMarkText(Graphics myg, Rectangle MarkRect)
        {
            Rectangle TextRect = new Rectangle();
            TextRect.X= MarkRect.X;
            TextRect.Y= MarkRect.Y;
           TextRect.Width= MarkRect.Width;
            TextRect.Height= MarkRect.Height;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            myg.DrawString(MarkText,MarkFont,new SolidBrush(MarkerTextColor), TextRect,sf);
           
        }

///////////////////////////////////////////////////////////////////////////////////////////////
    }
}
